package com.dolphin.directive;

import cn.hutool.core.collection.CollectionUtil;
import com.dolphin.commons.Constants;
import com.dolphin.model.Article;
import com.dolphin.service.ArticleService;
import com.jfinal.template.Env;
import com.jfinal.template.expr.ast.ExprList;
import com.jfinal.template.io.Writer;
import com.jfinal.template.stat.Scope;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 描述:
 * ${DESCRIPTION}
 *
 * @author 林平
 * @create 2022-10-06 19:42
 * 个人博客地址：https://www.nonelonely.com
 */
@TemplateDirective("notices")
@Component
public class NoticesDirective extends BaseDirective {
    private static ArticleService articleService;

    @Autowired
    public void setArticleService(ArticleService articleService) {
        NoticesDirective.articleService = articleService;
    }

    @Override
    public void setExprList(ExprList exprList) {
        super.setExprList(exprList);
    }

    @Override
    public void exec(Env env, Scope scope, Writer writer) {
        Article article = new Article();
        article.setType(Constants.ARTICLE_TYPE_NOTICE);
        article.setStatus(0);
        List<Article> list = articleService.list(article);
        article.setIsTop(1);
        List<Article> lists = articleService.list(article);
        //所有公告
        scope.set("notices", list);
        //弹窗公告
        if (CollectionUtil.isNotEmpty(lists)) {
            scope.set("noticeTop", lists.get(0));
        }
        stat.exec(env, scope, writer);
    }

    @Override
    public boolean hasEnd() {
        return true;
    }
}
